using System;
using System.Html;
using System.Serialization;
using Nsb.Classes;

namespace Nsb
{
//    [IgnoreNamespace]
    public class Storage
    {
        public static void SetLocal(string n, object v)
        {
            DbPackage(n, v, Window.LocalStorage);
        }

        public static void SetSession(string n, object v)
        {
            DbPackage(n, v, Window.SessionStorage);
        }

        public static object GetLocal(string n)
        {
            return DbUnpackage(n, Window.LocalStorage);
        }

        public static object GetSession(string n)
        {
            return DbUnpackage(n, Window.SessionStorage);
        }

        public static void RemoveLocal(string n)
        {
            try
            {
                Window.LocalStorage.RemoveItem(n);
            }
            catch (Exception)
            {
                Window.LocalStorage[n] = "";
            }
        }

        public static void RemoveSession(string n)
        {
            try
            {
                Window.SessionStorage.RemoveItem(n);
            }
            catch (Exception)
            {
                Window.SessionStorage[n] = "";
            }
        }

        public static bool CompareLocal(string n, object v)
        {
            return CompareObjects(DbUnpackage(n, Window.LocalStorage), v);
        }

        public static bool CompareObjects(object a, object b)
        {
            if (Script.IsNullOrUndefined(a) || Script.IsNullOrUndefined(b))
                return false;
            //Inform.Log("Compare Objects\n---A\n{0}\n---B\n{1}", Json.Stringify(a), Json.Stringify(b));
            return Json.Stringify(a) == Json.Stringify(b);
        }

        private static void DbPackage(string n, object v, System.Html.Data.Storage store)
        {
            switch (Type.GetScriptType(v))
            {
                case "object":
                    v = "@@object@@" + Strings.FnStringify(v);
                    break;
                default:
                    v = v.ToString();
                    break;
            }
            store.SetItem(n, v);
        }

        private static object DbUnpackage(string n, System.Html.Data.Storage store)
        {
            string v = (string)store.GetItem(n);
            if (Script.IsNull(v))
                return Script.Literal("undefined");
            if (((RegularExpression)Script.Literal(@"/^@@object@@|^\{.*\}$/")).Test(v))
            {
                int idx = v.Substr(0, 1) == "{" ? 0 : 10;
                try
                {
                    return Script.Eval('(' + v.Substr(idx) + ')');
                }
                catch (Exception ex)
                {
                    Inform.Log("Syntax error parsing stored \"" + n + "\" object: " + ex);
                }
                return Script.Literal("undefined");
            }
            return StringValue(v);
        }

        public static object StringValue(string v)
        {
            switch (v)
            {
                case "true":
                    return true;
                case "false":
                    return false;
                case "null":
                    return null;
                case "undefined":
                    return Script.Literal("undefined");
            }
            return v;
        }
    }
}
